package it.slkfarm.log;

import it.slkfarm.android.Base;
import it.slkfarm.android.Login;
import it.slkfarm.mail.MailSender;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class SendLog extends Service
{
	/**
	 * @uml.property  name="current"
	 */
	private Date current;
	/**
	 * @uml.property  name="end"
	 */
	private Date end;
	/**
	 * @uml.property  name="timer"
	 */
	private Timer timer;

	@Override
	public IBinder onBind(Intent arg0) 
	{
		return null;
	}
	
	@Override
	public void onCreate() 
	{

	}

	@Override
	public void onDestroy() 
	{
		timer.cancel();
	}
	
	@Override
	public void onStart(Intent intent, int startid) 
	{
		final String id = intent.getStringExtra(getPackageName()+".id");
		timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() 
			{
				MailSender mail = new MailSender("slkfarmlogger@gmail.com", "justasimplepassword");
				mail.setTo(new String[]{"slkfarmlogger@gmail.com"});
				mail.setFrom("slkfarmlogger@gmail.com");
				mail.setSubject("Log for SLKFARM");
				mail.setBody("Current log from devide: "+id);
				File log = new File("data/data/it.slkfarm.android/files/log - "+id+".txt");
				try 
				{
					mail.addAttachment(log.getAbsolutePath());
					mail.send();
					Log.i("service", "sendmail");
				} catch (Exception e) 
				{
					e.printStackTrace();
				}
				log.delete();
				try 
				{
					log.createNewFile();
				} catch (IOException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		},6000000, 6000000);
	}
	
}
